package com.woniuxy.mesboot.service.impl;

import com.woniuxy.mesboot.dto.PPDTO;
import com.woniuxy.mesboot.dto.ProDTO;
import com.woniuxy.mesboot.dto.ProInfoDTO;
import com.woniuxy.mesboot.entity.ProInf;
import com.woniuxy.mesboot.entity.ProcessMatIn;
import com.woniuxy.mesboot.entity.ProductProcess;
import com.woniuxy.mesboot.mapper.*;
import com.woniuxy.mesboot.service.ProInfoService;
import com.woniuxy.mesboot.vo.ProInfoVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 黑川
 * @version : 1.0
 * @Project : mes-boot
 * @Package : com.woniuxy.mesboot.service.impl
 * @ClassName : .java
 * @createTime : 2023/5/24 17:37
 * @Description :
 */
@Service
public class ProInfoServiceImpl implements ProInfoService {
    @Resource
    private ProInfMapper infMapper;

    @Resource
    private MesProcessMapper processMapper;

    @Resource
    private ProductProcessMapper productProcessMapper;

    @Resource
    private MesProcessDetailsMapper mesProcessDetailsMapper;

    @Resource
    private ProcessMatInMapper processMatInMapper;

    @Override
    public List<ProInfoVO> selectAll(ProInfoDTO infoDTO) {
        return infMapper.selectAll(infoDTO);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return infMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ProInf record) {
        return infMapper.insert(record);
    }

    @Override
    public int insertSelective(ProDTO record) {
        //产品表
        infMapper.insertSelective(record);
        String productName = record.getProductName();
        Integer productId = infMapper.selectIdByName(productName);
        if (record.getOperationProcedureId() != null) {
            ProductProcess productProcess = new ProductProcess();
            productProcess.setProductId(productId);
            productProcess.setOperationProcedureId(record.getOperationProcedureId());
            productProcessMapper.insertSelective(productProcess);
        } else {
            //工序流程表
            processMapper.insertSelective(record);
            String operationProcedureContent = record.getOperationProcedureContent();
            Integer operationProcedureId = processMapper.selectIdByName(operationProcedureContent);
//          //产品流程表
            ProductProcess productProcess = new ProductProcess();
            productProcess.setProductId(productId);
            productProcess.setOperationProcedureId(operationProcedureId);
            productProcessMapper.insertSelective(productProcess);
            List<PPDTO> ppdtoList = record.getPpdtoList();
            for (int i = 0; i < ppdtoList.size(); i++) {
                ppdtoList.get(i).setOperationProcedureId(operationProcedureId);
                mesProcessDetailsMapper.insertSelective(ppdtoList.get(i));
                List<ProcessMatIn> processMatInList = ppdtoList.get(i).getProcessMatInList();
                for (int j = 0; j < processMatInList.size(); j++) {
                    List<Integer> list = mesProcessDetailsMapper.selectOByOP(operationProcedureId, "领料");
                    for (int x = 0; x < list.size(); x++) {
                        processMatInList.get(j).setOperationId(list.get(x));
                        processMatInMapper.insertSelective(processMatInList.get(j));
                    }
                }
            }
        }
        return 1;
    }

    @Override
    public ProInf selectByPrimaryKey(Long id) {
        return infMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ProInf record) {
        return infMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ProInf record) {
        return infMapper.updateByPrimaryKey(record);
    }
}
